package com.sa.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @author Architecture Station
 * @version 1.0
 */
//@Configuration
public class RateLimiterConfiguration {

    /**
     * 使用IP地址作为限流的Key
     */
    //@Bean
    public KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
    }

    /**
     * 使用用户ID作为限流的Key，需要在用户登录成功后，将用户ID存入到请求头中，并转发给后端服务
     */
    //@Bean
    public KeyResolver userKeyResolver() {
        return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getHeaders().getFirst("userId")));
    }

    /**
     * 使用请求路径作为限流的Key
     */
    //@Bean
    public KeyResolver pathKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }
}










